package com.wunu.smartlock1.util;

import com.clj.fastble.data.BleDevice;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.google.gson.Gson;
import com.inuker.bluetooth.library.utils.RandomUtils;
import com.wunu.smartlock1.model.AuthorizedRecordVo;
import com.wunu.smartlock1.net.response.LockEvent;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by admin on 2018/4/30.
 */

public class CommonUtil {
    public  static boolean dump = true;//TemporyLockShowActivity 进入主界面时，是否需要判断剪切板内容并跳转到TemporyLockShowActivity
    public static  BleDevice device;
    public static Map<String,Object> map;//蓝牙绑定状态 mac与state
    public static Gson  gson = new Gson();
    public static Random random = new Random();
    public static Bitmap getImageBitmap(String url) {
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
            imgUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imgUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static  AuthorizedRecordVo transLockEvent(LockEvent event){
        AuthorizedRecordVo vo = new AuthorizedRecordVo();
        vo.setAuthorizedName(event.getName());
        vo.setAuthorizedPicUrl(event.getPhotoUrl());
        vo.setLockTime(event.getTime());
        vo.setMemberType(event.getGroup());
        vo.setState(event.getState());
        vo.setAuthorizedCode(event.getId()+"");
        return vo;
    }
    public static List<AuthorizedRecordVo> transLockEventList(List<LockEvent> eventList){
        List<AuthorizedRecordVo> list=new ArrayList<AuthorizedRecordVo>();
        for(int i=0;i<eventList.size();i++){
            list.add(transLockEvent(eventList.get(i)));
        }
        return list;
    }

    /**
     * 判断n是否素数
     * @param n
     * @return
     */
    public static boolean isPrime(int n){
        if (n<=0)return  false;
        if (n==1||n==2)return true;
        for (int i=2;i<=Math.sqrt(n);i++){
            if (n%i==0)
                return false;
        }
        return true;
    }

    /**
     * 随机生成8位数的素数
     * @return
     */
    public static int createBaseCode(){
        int count =0;
        int rand = random.nextInt(9999999)+10000000;
        while(!isPrime(rand)){
            System.out.println(rand+"不是素数，重新生成");
            count++;
            rand = random.nextInt(9999999)+10000000;
        }
        return rand;
    }

    /**
     * 8位数的素数向前找第n个素数
     * @param prime
     * @param n
     * @return
     */
    public static int getPrePrime(int prime,int n){
        int rand = 0;
        int nx = prime;
        while(n-->0){
            System.out.println("pre n: "+n);
            while(!isPrime(--nx)){
            }
            rand = nx;
//            System.out.println("第"+n+"个素数为:"+rand);
        }
        return rand;
    }
    /**
     * 8位数的素数向后找第n个素数
     * @param prime
     * @param n
     * @return
     */
    public static int getSufPrime(int prime,int n){
        int rand = 0;
        int nx = prime;
        while(n-->0){
            System.out.println("suf n: "+n);
            while(!isPrime(++nx)){
            }
            rand = nx;
//            System.out.println("第"+n+"个素数为:"+rand);
        }
        return rand;
    }
    /**
     *  取得当前时间（格式为：yyy-MM-dd HH:mm:ss）
     * @return String
     */
    public static String GetDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf.format(new Date());
        return sDate;
    }

    /**
     * 现在是否在begin开始后的validTime秒有效期内,true在，false过期
     * @param begin
     * @param validTime
     * @return
     */
    public static boolean compareDate(String begin, int validTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long beginDate=0;
        long endDate=0;
        try {
            beginDate = sdf.parse(begin).getTime();
            endDate = sdf.parse(sdf.format(new Date())).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (((endDate - beginDate)/1000)-validTime)<0;
    }

}
